home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE02 / TPACK / TPACK.ZIP / PASUTILS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-06-01  |  9.7 KB  |  302 lines

  1. {------------------------------------------------------------------------------}
  2. {UNREGISTERED VERSION (6/1/95) PLEASE REDISTRIBUTE IN tPACK.ZIP!
  3.  This revision does not contain everything, nor are the exciting
  4.  DataSetReporter and ExtendedMenu[Item] components included.
  5.  Use SWREG#5906 to receive these, icons and a help file for $130.
  6.  You must register when using this code in a business application!
  7.  You'll receive a license to use this code in up to 50 copies of
  8.  any app you write. In turn you will get responsive e-mail
  9.  tech support and enhancements till I run out of registrations
  10.  or suggestions. Meanwhile.. enjoy the code. Bye! I'll make more.
  11.  {(C)'1995 Michael/Ax-Systems, 71560,1754@Compuserve.com}
  12. {------------------------------------------------------------------------------}
  13.  
  14. unit PasUtils;
  15. {------------------------------------------------------------------------------}
  16. { UTILITY LIBRARY                                                   }
  17. {------------------------------------------------------------------------------}
  18.  
  19.  
  20. interface
  21.  
  22. uses
  23.   Classes, ExtCtrls, Forms, TypInfo;
  24.  
  25. type
  26.  
  27.   TExceptionReAction = (reAsk, reRetry, reIgnore, reRaise);
  28.     {defines what Retry/ErrorMsg can do in response to an exception}
  29.  
  30.   TRealRecord = record
  31.     {utility type used to set reals to 0 or to check if they are.}
  32.     {this way from pascal mag, uses 5 instead of 18 bytes and fewer cycles.}
  33.     {set a real to zero:     TRealRecord(RealVar).Exponent:=0; }
  34.     {does a real equal zero: TRealRecord(RealVar).Exponent=0; }
  35.     Exponent: Byte;
  36.     Mantissa: Array[1..5] of Byte;
  37.     end;
  38.  
  39. {------------------------------------------------------------------------------}
  40. { UTILITY PROCEDURE DECLARATIONS                                               }
  41. {------------------------------------------------------------------------------}
  42.  
  43. Procedure CursorOff;                                     { Turn the cursor Off }
  44. Procedure CursorOn;                                      { Turn the Cursor On }
  45.  
  46. function TrailingChar(const Value:String;Trailer:Char):String; {insures a trailing character}
  47. function TrailingBackSlash(const Value:String):String;         {insures a trailing '\'}
  48.  
  49. procedure SplitString(const Input:String;SplitAt:Char; var Left,Right:String); {splits at char}
  50.  
  51. procedure LongintsLowHigh(var Low,High:LongInt);
  52.  
  53. function Max(i,j:longint):longint;
  54. function Min(i,j:longint):longint;
  55.  
  56. function ExpXY(x,y:extended):extended;
  57.  
  58. function FormatNumber(l:LongInt): String;
  59.  
  60.  
  61. function FormatCurrency(value:real):string;
  62.  
  63. function StripString(Input:String;StripChar:Char):String;
  64.  
  65. function Spaces(n:byte):string;
  66.  
  67. function GetEnumString(TypeInfo:PTypeInfo;Ordinal:longint):String;
  68.  
  69. function MakePChar(const Value:String):PChar;
  70. procedure MovePChar2PString(Dest:PString;Source:PChar;aFree:Boolean);
  71. procedure FreePChar(Value:PChar);
  72. function ReceivePChar(Value:PChar):String;
  73.  
  74. function LeftPadZero(const Value:String; Length:byte):string;
  75.  
  76.  
  77. const
  78.   BoolString:array[false..true] of string[5]=('FALSE','TRUE');
  79.  
  80.  
  81. {------------------------------------------------------------------------------}
  82. { PASCAL UTILITY IMPLEMENTATION                                                }
  83. {------------------------------------------------------------------------------}
  84. implementation
  85.  
  86. uses
  87.   WinProcs
  88.   ,SysUtils;
  89.  
  90. {------------------------------------------------------------------------------}
  91. { CURSOR ON/OFF                                                                }
  92. {------------------------------------------------------------------------------}
  93.  
  94. Procedure CursorOff;                                       { Turn the Cursor Off }
  95. Var
  96.   Cstate : Integer;                                        { Current cursor State }
  97. Begin
  98.   Cstate := ShowCursor(True);                              { Get State }
  99.   While Cstate >= 0 do Cstate := ShowCursor(False);        { While ON turn Off }
  100. End;
  101.  
  102. Procedure CursorOn;                                        { Turn Cursor On }
  103. Var
  104.   Cstate : Integer;                                        { Current cursor State }
  105. Begin
  106.   Cstate := ShowCursor(True);                              { Get current State }
  107.   While Cstate < 0 do Cstate := ShowCursor(True);          { While off turn on }
  108. End;
  109.  
  110. {------------------------------------------------------------------------------}
  111. { TRAILING CHARACTER, TRAILING BACKSLASH                                       }
  112. {------------------------------------------------------------------------------}
  113.  
  114. function TrailingChar(const Value:String;Trailer:Char):String; {insures a trailing character}
  115. begin
  116.   Result:=Value;
  117.   if copy(Value,length(Value),1)<>Trailer then
  118.     Result:=Result+Trailer;
  119. end;
  120.  
  121. function TrailingBackSlash(const Value:String):String; {insures a trailing '\'}
  122. begin
  123.   Result:=TrailingChar(Value,'\');
  124. end;
  125.  
  126. {------------------------------------------------------------------------------}
  127. { SPLIT STRING AT CHARACTER                                                    }
  128. {------------------------------------------------------------------------------}
  129.  
  130. procedure SplitString(const Input:String;SplitAt:Char; var Left,Right:String);
  131. {splits 'input' at 'splitchar' into 'left' and 'right' parts}
  132. var n:integer;
  133. begin
  134. n:=pos(SplitAt,Input);
  135. if n=0 then begin
  136.   left:=Input;
  137.   Right:='';
  138.   end
  139. else begin
  140.   Left:=Copy(Input,1,n-1);
  141.   Right:=Copy(Input,n+1,length(Input)-n);
  142.   end;
  143. end;
  144.  
  145. {---------------------------------------------------------------------------}
  146.  
  147. function StripString(Input:String;StripChar:Char):String;
  148. {removes 'StripChar' from 'Input'}
  149. var n:integer;
  150. begin
  151.   n:=pos(StripChar,Input);
  152.   while n>0 do begin
  153.     Input:=Copy(Input,1,n-1)+Copy(Input,n+1,length(Input)-n);
  154.     n:=pos(StripChar,Input);
  155.   end;
  156.   Result:=Input;
  157. end;
  158.  
  159. {------------------------------------------------------------------------------}
  160. { SWAP LONGINTS FOR PROPER HIGH/LOW                                            }
  161. {------------------------------------------------------------------------------}
  162.  
  163. procedure LongintsLowHigh(var Low,High:LongInt);
  164. var
  165.   i:longint;
  166. begin
  167.   if Low>High then begin
  168.     i:=low;
  169.     Low:=High;
  170.     High:=i;
  171.     end;
  172. end;
  173.  
  174. {------------------------------------------------------------------------------}
  175. { GET HIGH/LOW                                                                 }
  176. {------------------------------------------------------------------------------}
  177.  
  178. function Max(i,j:longint):longint;
  179. begin
  180.   if i>j then
  181.     Result:=i
  182.   else
  183.     Result:=j;
  184. end;
  185.  
  186. function Min(i,j:longint):longint;
  187. begin
  188.   if i<j then
  189.     Result:=i
  190.   else
  191.     Result:=j;
  192. end;
  193.  
  194. {------------------------------------------------------------------------------}
  195. { MATH FUNCTIONS                                                               }
  196. {------------------------------------------------------------------------------}
  197.  
  198. function ExpXY(x,y:extended):extended;
  199. begin
  200.   result:=Exp(y*ln(x));
  201. end;
  202.  
  203. {------------------------------------------------------------------------------}
  204. { STRING FORMATING FUNCTIONS                                                   }
  205. {------------------------------------------------------------------------------}
  206.  
  207. function FormatNumber(l:LongInt): String;
  208. begin
  209.   Result:= FormatFloat('###,###,###,##0.00',StrToFloat(IntToStr(l)));
  210. end;
  211.  
  212. function FormatCurrency(value:real):string;
  213. var
  214.   s, s2 :string;
  215.   n: integer;
  216.   minusflag : boolean;
  217. begin
  218.   minusflag:=(value<0);
  219.   s:=format('%.2f',[abs(value)]);
  220.   s2:=copy(s,length(s)-2,3);
  221.   s:=copy(s,1,length(s)-3);
  222.   n:=length(S);
  223.   while n>3 do
  224.   begin
  225.     s2:=','+copy(s,n-2,3)+s2;
  226.     n:=n-3;
  227.   end;
  228.   if n>0 then
  229.   begin
  230.     s2:=copy(s,1,n)+s2;
  231.   end;
  232.   if minusflag then
  233.     result:='$-'+s2
  234.   else
  235.     result:='$'+s2;
  236. end;
  237.  
  238. {------------------------------------------------------------------------------}
  239. {  ADDS ZEROS TO FRONT OF STRING                                               }
  240. {------------------------------------------------------------------------------}
  241.  
  242. function LeftPadZero(const Value:String; Length:byte):string;
  243. begin
  244.   Result:=Value;
  245.   while ord(Result[0]) < Length do
  246.     Result:='0'+Value;
  247. end;
  248.  
  249.  
  250. {------------------------------------------------------------------------------}
  251. {  RETURNS N SPACES                                                            }
  252. {------------------------------------------------------------------------------}
  253.  
  254. function spaces(n:byte):string;
  255. begin
  256.   Result:='';
  257.   while n>0 do begin
  258.     dec(n);
  259.     Result:=Result+' ';
  260.     end;
  261. end;
  262.  
  263. {------------------------------------------------------------------------------}
  264. {  TYPEINFO HOW TO REMINDER PROC                                               }
  265. {------------------------------------------------------------------------------}
  266.  
  267. function GetEnumString(TypeInfo:PTypeInfo;Ordinal:longint):String;
  268. begin
  269.   Result:=GetEnumName(TypeInfo,Ordinal)^;
  270. end;
  271.  
  272. {------------------------------------------------------------------------------}
  273. {  PCHAR AND PSTRING UTILITIES                                                 }
  274. {------------------------------------------------------------------------------}
  275.  
  276. function MakePChar(const Value:String):PChar;
  277. begin
  278.   GetMem(Result,256);           {make room for a pascal maxlen pchar}
  279.   StrPCopy(Result,Value);       {copy string passed into buffer}
  280. end;
  281.  
  282. procedure FreePChar(Value:PChar);
  283. begin
  284.   FreeMem(Value,256);
  285. end;
  286.  
  287. function ReceivePChar(Value:PChar):String;
  288. begin
  289.   Result:=StrPas(Value);
  290.   FreePChar(Value);
  291. end;
  292.  
  293. procedure MovePChar2PString(Dest:PString;Source:PChar;aFree:Boolean);
  294. begin
  295.   AssignStr(Dest,StrPas(Source));
  296.   if aFree then
  297.     FreePChar(Source);
  298. end;
  299.  
  300.  
  301. end.
  302.